﻿Imports System.IO
Imports System.IO.Ports.SerialPort
'Dung
Public Class ELECSYS2010

#Region "Structures"
    'Public Structure ResultAspectStruct
    '    Public Aspect As String
    '    Public DataValue As String
    '    Public Unit As String
    'End Structure
    Public Structure ResultStruct
        Dim TestCode As String
        Public DataValue As String
        Public Unit As String
        Dim DateTestCompleted As String
    End Structure
    Public Structure Patient
        Dim PatientName() As String
        Dim Age As String
        Dim Sex As String
        Dim DateOfBirth As String
        Dim PhysicianID As String
        Dim Location As String
        Dim PID As String
        Dim SpecimenID As String
        Dim ResultArray() As ResultStruct
        Dim Comment As String
    End Structure
#End Region
    Private Const ComponentDelimiter As Char = "^"
    Private Const RecordDelimiter As Char = vbCr
    Private Const FrameDelimiter As Char = vbCrLf
    Private Const FieldDelimiter As String = "|"
    Private Const RepeatDelimiter As String = "\"
    Public Const ETB As Byte = &H17
    Public Const OrderRecordType As String = "O"
    Private Const PatientRecordType As String = "P"
    Private Const ResultRecordType As String = "R"
    Public Function GetDataWithoutDataType(ByVal pDatalist() As String) As Int16
        Dim icount As Byte = 0
        Dim bResult As Boolean = False
        For icount = 0 To pDatalist.GetLength(0) - 1
            If pDatalist(icount) <> [String].Empty Then
                Return icount
            End If
        Next
    End Function
    Public Function FrameCheck(ByVal sFrameInput As String, ByRef FinalFrame As Boolean, ByRef sDataSegmentOutput As String) As Boolean
        Dim checksum1, checksum2 As Byte
        Dim sTemp As String
        checksum1 = Convert.ToByte(sFrameInput.Substring(sFrameInput.Length - 2), 16)
        sTemp = sFrameInput.Remove(sFrameInput.Length - 2)
        sTemp = sTemp.Substring(1)
        checksum2 = StringChecksum(sTemp)
        If checksum2 <> checksum1 Then
            Return False
        Else
            sDataSegmentOutput = sTemp.Remove(sTemp.Length - 1)     'Loc bo ETX hoac ETB
            sDataSegmentOutput = sDataSegmentOutput.Substring(1)            'Loc bo Frame No.
            If Asc(Convert.ToChar(sTemp.Substring(sTemp.Length - 1))) = ETB Then
                FinalFrame = False
            Else
                FinalFrame = True
            End If
            Return True
        End If
    End Function
    Public Function GetQuery(ByVal sDataSegment As String, ByRef sQueryOutput As String, ByRef DataPosittion As String, ByRef CancelQuery As Boolean) As Boolean
        Dim sRecordTemp As String()
        Dim sTemp1, sTemp2 As String()
        Dim s As String
        Dim NotQuery As Boolean = True
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "Q" Then
                If sTemp1(2).Length > 0 Then
                    sTemp2 = sTemp1(2).Split(ComponentDelimiter)
                    sQueryOutput = sTemp2(1)
                    DataPosittion = sTemp1(2).Substring(sQueryOutput.Length + 1)
                    CancelQuery = False
                Else
                    CancelQuery = True
                End If
                If NotQuery Then
                    NotQuery = False
                End If
                Exit For
            End If
        Next
        Return (Not NotQuery)
    End Function
    Public Function GetResult(ByVal sDataSegment As String, ByRef sRecordOutput() As String) As Boolean
        Dim sTemp1 As String()
        Dim s As String
        Dim NotResult As Boolean = True
        sRecordOutput = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordOutput
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = ResultRecordType Then
                If NotResult Then
                    NotResult = False
                End If
                Exit For
            End If
        Next
        Return (Not NotResult)
    End Function
    Public Function GetTerminationStatus(ByVal sDataSegment As String) As Boolean
        Dim sRecordTemp As String()
        Dim NotEnd As Boolean = True
        Dim sTemp1 As String()
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s As String In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "L" Then
                'If sTemp1(2).Length > 0 And sTemp1(2) = "F" Then
                NotEnd = False
                Exit For
                'End If
            End If
        Next
        Return (Not NotEnd)
    End Function

    Public Function CreatOrderRecord(ByVal SeqNo As Byte, ByVal SID As String, ByVal RackNoID As String, ByVal SamplePosID As Char, ByVal TestID As String(), ByVal DP As String) As String
        Dim sTemp(0 To 25) As String
        Dim sTemp2(0 To 2) As String
        Dim i As Byte
        Dim s As String = ""
        For i = 0 To 25
            sTemp(i) = ""
        Next i
        '  s = [String].Join(FieldDelimiter, sTemp)
        'For i = 0 To 2
        '    sTemp2(i) = ""
        'Next i
        'sTemp(2) = [String].Join(ComponentDelimiter, sTemp2)
        sTemp(0) = OrderRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        sTemp2(0) = SID
        If Not [String].IsNullOrEmpty(RackNoID) Then
            sTemp2(1) = RackNoID
            If Not [String].IsNullOrEmpty(SamplePosID) Then
                sTemp2(2) = SamplePosID
                sTemp(2) = [String].Join(ComponentDelimiter, sTemp2)
            Else
                sTemp(2) = [String].Join(ComponentDelimiter, sTemp2, 0, 2)
            End If

        Else
            sTemp(2) = SID
        End If


        'For i = 0 To TestID.Length - 1
        '    TestID(i) = [String].Concat("^^^", TestID(i))
        'Next
        'sTemp(3) = dp
        sTemp(4) = [String].Join(RepeatDelimiter, TestID)
        sTemp(5) = "R" 'Routine
        sTemp(11) = "N"
        sTemp(25) = "Q" ' Order and Response to query
        s = [String].Join(FieldDelimiter, sTemp)
        Return s
    End Function
    Public Function CreatePatientRecord(ByVal SeqNo As Byte, ByVal PID As String, ByVal LastName As String, ByVal FirstName As String, ByVal MiddleInitial As String, ByVal DateofBirth As String, ByVal Sex As Char, ByVal PhysicianID As String, ByVal Location As String) As String
        Dim sTemp(0 To 25), stemp2(0 To 2) As String
        sTemp(0) = PatientRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        If PID.Length > 0 Then
            sTemp(2) = PID
        End If

        sTemp(5) = FirstName

        If DateofBirth.Length > 0 Then
            sTemp(7) = DateofBirth
        End If
        sTemp(8) = Sex.ToString()
        sTemp(13) = PhysicianID
        sTemp(25) = Location
        Return [String].Join(FieldDelimiter, sTemp)
    End Function
    Public Function CreateTerminationRecord(ByVal SeqNo As Byte, ByVal TerminationCode As String) As String
        Dim sTemp(0 To 2) As String
        sTemp(0) = TerminationRecordType
        If SeqNo > 0 Then
            sTemp(1) = SeqNo.ToString()
        Else
            MsgBox("Error In Sequence Number")
            Return [String].Empty
        End If
        sTemp(2) = TerminationCode
        Return [String].Join(FieldDelimiter, sTemp)
    End Function
    Public Function CreateSinglePatientFrame(ByVal sHeaderRecord As String, ByVal sPatientRecord As String, ByVal sOrderRecord As String, ByVal sTerminationRecord As String) As String
        Dim sTemp, sChecksum As String
        Dim Checksum As Byte = 0
        sTemp = [String].Concat(Chr(2), "1", sHeaderRecord, vbCr)
        If sPatientRecord <> String.Empty And sOrderRecord <> String.Empty Then
            sTemp = [String].Concat(sTemp, sPatientRecord, vbCr, sOrderRecord, vbCr)
        End If
        sTemp = [String].Concat(sTemp, sTerminationRecord, vbCr, Chr(3))
        Checksum = StringChecksum(sTemp.Substring(1))
        sChecksum = Checksum.ToString("X02")
        'sTemp = sTemp & sChecksum & vbCrLf
        Return [String].Concat(sTemp, sChecksum, vbCrLf)
    End Function

    Private Function StringChecksum(ByVal sInput As String) As Byte
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod &H100
        Next
        Return bChecksum
    End Function

    Public Sub ResultRecordParse(ByVal sInput() As String, ByRef UniversalTestID() As String, ByRef DataValue As String, ByRef Units As String, ByRef AllergyClassRange As String, ByRef ResultAbnormalFlags As String, ByRef ResultStatus As String, ByRef DateTestCompleted As String)
        UniversalTestID = sInput(2).Split(ComponentDelimiter)
        If InStr(sInput(3), "^") Then
            Dim tmp_value() As String = sInput(3).Split(ComponentDelimiter)
            DataValue = tmp_value(1)
        Else
            DataValue = sInput(3)
        End If
        Units = sInput(4)
        AllergyClassRange = sInput(5)
        ResultAbnormalFlags = sInput(6)
        ResultStatus = sInput(8)
        DateTestCompleted = sInput(12)
    End Sub
    Public Sub OrderRecordParse(ByVal sInput() As String, ByRef SpecimenID As String)
        Try
            SpecimenID = sInput(2)
        Catch ex As Exception

        End Try
    End Sub

    'Public Sub PatientRecordParse(ByVal sInput() As String, ByRef PID As String, ByRef PatientName() As String, ByRef DOB As String, ByRef Sex As Char, ByRef PhysicianID As String, ByRef Location As String)
    '    Dim iCount As Byte = sInput.GetLength(0)
    '    If iCount > 2 Then
    '        PID = sInput(2)
    '    End If
    '    If iCount > 5 Then
    '        PatientName = sInput(5).Split(ComponentDelimiter)
    '    End If
    '    If iCount > 7 Then
    '        DOB = sInput(7)
    '    End If
    '    If iCount > 8 Then
    '        Sex = sInput(8)
    '    End If
    '    If iCount > 13 Then
    '        PhysicianID = sInput(13)
    '    End If
    '    If iCount > 25 Then
    '        Location = sInput(25)
    '    End If

    'End Sub

    Public Sub ResultDataParse(ByVal sInput() As String, ByRef PatientClass As PatientInformation)
        'Dim sTemp() As String = sInput.Split(RecordDelimiter)
        Dim sTemp2() As String = {}
        Dim s() As String
        Dim NoOfTest As Byte = 0
        Dim NoOfResult As Byte = 0
        Dim Aspect As Byte = 0
        Dim UniversalTestID() As String = {}
        Dim DataValue As String = [String].Empty
        Dim Units As String = [String].Empty
        Dim AllergyClassRange As String = [String].Empty
        Dim AbnormalFlags As String = [String].Empty
        Dim ResultStatus As String = [String].Empty
        Dim DateTestCompleted As String = [String].Empty
        'Dim Finded As Boolean = False
        With PatientClass
            For i As Byte = 0 To sInput.GetLength(0) - 1
                s = sInput(i).Split(FieldDelimiter)
                Select Case s(0)
                    'Case PatientRecordType
                    '    PatientRecordParse(s, .PID, .PatientName, .DateOfBirth, .Sex, .PhysicianID, .Location)
                    Case OrderRecordType
                        OrderRecordParse(s, .SpecimenID)
                    Case ResultRecordType
                        ResultRecordParse(s, UniversalTestID, DataValue, Units, AllergyClassRange, AbnormalFlags, ResultStatus, DateTestCompleted)
                        Dim tmp As New PatientInformation.ResultStruct
                        With tmp
                            .TestCode = UniversalTestID(3)
                            .DataValue = DataValue
                            .Unit = Units
                            .DateTestCompleted = DateTestCompleted
                        End With
                        .ResultArray.Add(tmp)
                End Select
            Next
        End With
    End Sub
    'Public Function frame(ByVal sInput As String) As String() 'ham tra lai cac frame
    '    Dim sTemp As String = ""
    '    Dim delimiterchar As Char() = {Chr(2), Chr(10)} 'Kí tự STX và LF
    '    ' Dim delimiterchar As String = Chr(10)
    '    Dim sTempstring() As String

    '    Dim empty() As String = {}
    '    Dim iIndex As Long = 0
    '    ' Dim iIndex2 As Integer = 0
    '    Dim iIndexs As Long = 0
    '    Dim iIndexofSTX As Integer = 0
    '    Dim iIndexofETX As Integer = 0
    '    Dim iIndexofETB As Integer = 0
    '    Dim iIndexofLF As Integer = 0
    '    Dim a As String = sInput

    '    sTempstring = a.Split(delimiterchar)

    '    Dim s As String
    '    For Each s In sTempstring
    '        If (s.Length() > 3) Then

    '            iIndex = iIndex + 1

    '        End If

    '    Next
    '    If iIndex - 1 >= 0 Then
    '        Dim sString(iIndex - 1) As String
    '        iIndex = 0
    '        While 1
    '            iIndexofSTX = sInput.IndexOf(Chr(2), iIndexofSTX)
    '            iIndexofLF = sInput.IndexOf(Chr(10), iIndexofLF)
    '            If ((iIndexofLF = -1) Or (iIndexofSTX = -1)) Then
    '                iIndex = iIndex - 1
    '                Exit While
    '            End If

    '            sString(iIndex) = sInput.Substring(iIndexofSTX, iIndexofLF - iIndexofSTX + 1)
    '            iIndexofSTX = iIndexofSTX + 1
    '            iIndexofLF = iIndexofLF + 1
    '            iIndex = iIndex + 1
    '        End While


    '        Return sString
    '    Else
    '        Return empty
    '    End If
    'End Function 'Tách frame
    'Public Function checksum(ByVal sFrame As String) As Boolean
    '    Dim sTemp As String = ""
    '    Dim sTemochecksum As String = ""
    '    Dim modlu As Integer = 0
    '    Dim iChuc As Integer = 0
    '    Dim iDonvi As Integer = 0
    '    Dim sTempmodlu As String = ""
    '    Dim iIndexofCR As Integer = 0
    '    Dim iIndexofETB As Integer = 0
    '    Dim iIndexofETX As Integer = 0
    '    If (sFrame.IndexOf(Chr(2)) = -1) Then
    '        Return False
    '    Else
    '        If (sFrame.IndexOf(Chr(13)) = -1) Then
    '            Return False
    '        Else

    '            If ((sFrame.IndexOf(Chr(23)) = -1) And (sFrame.IndexOf(Chr(3)) = -1)) Then
    '                Return False
    '            Else
    '                sTemp = subString(sFrame)
    '                Dim iTemp As Integer = 0
    '                Dim arr As Char() = sTemp.ToCharArray(0, sTemp.Length())
    '                Dim c As Char
    '                For Each c In arr
    '                    iTemp = iTemp + Asc(c)
    '                Next
    '                modlu = iTemp Mod 256

    '                iChuc = Math.Floor((modlu / 16))


    '                'iChuc = modlu / 16
    '                iDonvi = modlu Mod 16
    '                sTempmodlu = Tohexa(iChuc) + Tohexa(iDonvi)
    '                iIndexofCR = sFrame.IndexOf(Chr(10))
    '                iIndexofETB = sFrame.IndexOf(Chr(23))
    '                iIndexofETX = sFrame.IndexOf(Chr(3))
    '                If (iIndexofETB <> -1) Then
    '                    sTemochecksum = sFrame.Substring(iIndexofETB + 1, iIndexofCR - iIndexofETB - 2)
    '                Else
    '                    If (iIndexofETX <> -1) Then
    '                        sTemochecksum = sFrame.Substring(iIndexofETX + 1, iIndexofCR - iIndexofETX - 2)

    '                    End If

    '                End If
    '                If (sTempmodlu = sTemochecksum) Then
    '                    Return True
    '                Else
    '                    Return False

    '                End If
    '            End If
    '        End If
    '    End If

    'End Function 'Kiểm tra checksum của mỗi frame
    'Private Function Tohexa(ByVal input As Integer) As String
    '    Dim temp As String = ""
    '    If input < 10 Then
    '        Return input.ToString()
    '    Else
    '        Select Case input
    '            Case 10
    '                temp = "A"
    '            Case 11
    '                temp = "B"
    '            Case 12
    '                temp = "C"
    '            Case 13
    '                temp = "D"
    '            Case 14
    '                temp = "E"
    '            Case 15
    '                temp = "F"
    '            Case Else
    '        End Select
    '        Return temp
    '    End If

    'End Function 'Chuyển số interger sang dạng hexa
    'Private Function subString(ByVal sFrame As String) As String
    '    Dim sTemp As String = ""
    '    Dim iIndexofSTX As Integer = 0
    '    Dim iIndexofETX As Integer = 0
    '    Dim iIndexofETB As Integer = 0
    '    Dim iIndexofCR As Integer = 0
    '    iIndexofSTX = sFrame.IndexOf(Chr(2))
    '    iIndexofETB = sFrame.IndexOf(Chr(23))
    '    iIndexofETX = sFrame.IndexOf(Chr(3))
    '    If (iIndexofETB = -1) Then
    '        Return sFrame.Substring(iIndexofSTX + 1, iIndexofETX - iIndexofSTX)
    '    Else

    '        If (iIndexofETX = -1) Then
    '            Return sFrame.Substring(iIndexofSTX + 1, iIndexofETB - iIndexofSTX)
    '        End If
    '    End If
    '    Return sTemp
    'End Function 'loại bỏ các kí tự header và trailer của frame để phục vụ hàm checksum
    'Public Function CleanString(ByVal sFrame() As String) As String
    '    Dim sTemp As String = ""
    '    Dim Temp As String = ""
    '    Dim sTemplast As String = ""
    '    Dim sTemfirst As String = ""
    '    Dim iIndexofSTX As Integer = 0
    '    Dim iIndexofETB As Integer = 0
    '    Dim iIndexofETX As Integer = 0
    '    Dim s As String
    '    For Each s In sFrame
    '        iIndexofSTX = s.IndexOf(Chr(2), iIndexofSTX)
    '        iIndexofETX = s.IndexOf(Chr(3), iIndexofETX)
    '        iIndexofETB = s.IndexOf(Chr(23), iIndexofETB)
    '        If (iIndexofETB <> -1) Then
    '            Temp = s.Substring(iIndexofSTX, iIndexofETB - iIndexofSTX)
    '            sTemp = sTemp + Temp

    '        End If
    '        If (iIndexofETX <> -1) Then
    '            Temp = s.Substring(iIndexofSTX + 2, iIndexofETX - iIndexofSTX - 2)
    '            sTemp = sTemp + Temp

    '        End If
    '        iIndexofSTX = 0
    '        iIndexofETB = 0
    '        iIndexofETX = 0
    '    Next

    '    Return sTemp
    'End Function 'Khôi phục String ban đầu, trước khi bi chia thành các frame
    'Public Function Records(ByVal message As String) As String()
    '    Dim sTemp() As String = {}
    '    Dim index As Integer = 0
    '    Dim delimiterchar As Char() = {Chr(2), Chr(13)} 'Kí tự STX và CR
    '    sTemp = message.Split(delimiterchar)
    '    Dim s As String
    '    For Each s In sTemp
    '        If (s.Length <> 0) Then
    '            index = index + 1
    '        End If
    '    Next
    '    Dim Temp(index - 1) As String
    '    index = -1
    '    Dim ss As String
    '    For Each ss In sTemp
    '        If (ss.Length <> 0) Then
    '            index = index + 1
    '            Temp(index) = ss
    '        End If
    '    Next

    '    Return Temp
    'End Function ' Tách thành các record chứa dữ liệu
    'Private Function IndexofRecord(ByVal Record As String, ByVal index As Integer, ByVal charSplit As Char, ByRef Result As String) As Boolean
    '    Dim sTemp As String = ""
    '    Dim i As Integer = 1
    '    Dim iIndex As Integer = -1
    '    Dim iIndexfirst As Integer = 0
    '    Dim iIndexlast As Integer = 0

    '    While 1
    '        iIndex = Record.IndexOf(charSplit, iIndex + 1)
    '        If (iIndex <> -1) Then
    '            i = i + 1
    '            'iIndex = iIndex + 1
    '        Else
    '            Exit While
    '        End If


    '    End While
    '    If (i < index) Then
    '        Return False
    '    Else
    '        i = 1
    '        iIndex = -1
    '        While 1
    '            iIndex = Record.IndexOf(charSplit, iIndex + 1)
    '            If (iIndex <> -1) Then
    '                i = i + 1
    '                'iIndex = iIndex + 1
    '            End If
    '            If (i = index) Then
    '                Exit While
    '            End If
    '        End While
    '        ''  iIndex = iIndex - 1
    '        iIndexfirst = Record.IndexOf(charSplit, iIndex)
    '        iIndexlast = Record.IndexOf(charSplit, iIndex + 1)
    '        If (iIndexlast = -1) Then
    '            iIndexlast = Record.Length
    '        End If
    '        Result = Record.Substring(iIndexfirst + 1, iIndexlast - iIndexfirst - 1)
    '        Return True

    '    End If


    'End Function ' Trả lại giá trị của một trường trong một Record hoặc trong một Field
    'Public Sub Patient(ByVal Precord() As String, ByRef PatientID As String, ByRef PatientSex As String, ByRef ten As String, ByRef ho As String, ByRef dem As String, ByRef PatientBirth As String)
    '    Dim sTemp As String = ""
    '    Dim index As Integer = 0
    '    Dim PatientName() As String
    '    Dim delimiterchar As Char() = {"^"c}
    '    Dim s As String = ""

    '    For Each s In Precord
    '        If ((s(0) = "P"c) Or (s(0) = "p"c)) Then
    '            sTemp = s
    '            Exit For
    '        End If
    '    Next
    '    If (IndexofRecord(s, 3, "|"c, PatientID)) Then
    '    Else
    '        PatientID = ""
    '    End If

    '    If (IndexofRecord(s, 9, "|"c, PatientSex)) Then
    '    Else
    '        PatientSex = ""
    '    End If
    '    If (IndexofRecord(s, 6, "|"c, sTemp)) Then
    '        If (sTemp.Length > 1) Then
    '            PatientName = sTemp.Split(delimiterchar)
    '            ten = PatientName(0)
    '            ho = PatientName(1)
    '            dem = PatientName(2)

    '        End If
    '    End If
    '    If (IndexofRecord(s, 8, "|"c, sTemp)) Then
    '        If (sTemp.Length > 1) Then
    '            'Dim stempbirth(2) As String
    '            'stempbirth(0) = sTemp.Substring(0, 4)
    '            'stempbirth(1) = sTemp.Substring(4, 2)
    '            'stempbirth(2) = sTemp.Substring(6, 2)
    '            PatientBirth = sTemp

    '        End If
    '    End If
    'End Sub 'trả lại thông tin chung của bệnh nhân
    'Public Function SampleID(ByVal Records() As String) As String
    '    Dim sTemp As String = ""
    '    Dim result As String
    '    Dim sTempSampleID As String = ""
    '    Dim delimiterchar As Char() = {"^"c}
    '    Dim s As String
    '    For Each s In Records
    '        If ((s(0) = "O"c) Or (s(0) = "o"c)) Then
    '            sTemp = s
    '            Exit For
    '        End If
    '    Next
    '    If (IndexofRecord(sTemp, 3, "|"c, sTempSampleID)) Then
    '        If (sTempSampleID.Length > 1) Then
    '            result = sTempSampleID
    '        End If
    '    End If
    '    Return result
    'End Function 'trả lại thông tin Sample ID
    'Public Function Testdate(ByVal Records() As String) As String()
    '    Dim sTemp As String = ""
    '    Dim sTempdate As String = ""
    '    Dim result(5) As String
    '    Dim s As String
    '    For Each s In Records
    '        If ((s(0) = "R"c) Or (s(0) = "r"c)) Then
    '            sTemp = s
    '            Exit For
    '        End If
    '    Next
    '    If (IndexofRecord(sTemp, 13, "|"c, sTempdate)) Then
    '        If (sTempdate.Length > 1) Then

    '            result(0) = sTempdate.Substring(0, 4)
    '            result(1) = sTempdate.Substring(4, 2)
    '            result(2) = sTempdate.Substring(6, 2)
    '            result(3) = sTempdate.Substring(8, 2)
    '            result(4) = sTempdate.Substring(10, 2)
    '            result(5) = sTempdate.Substring(12, 2)
    '        End If
    '    End If
    '    Return result
    'End Function 'Trả lại thông tin ngày tiến hành xét nghiệm
    'Private Function Test(ByVal ResultRecord As String) As String
    '    Dim tempresult As String = ""
    '    Dim temp As String = ""
    '    Dim s As String = ""

    '    If (IndexofRecord(ResultRecord, 3, "|"c, temp)) Then
    '        If (temp.Length > 1) Then
    '            IndexofRecord(temp, 4, "^"c, s)
    '            tempresult = s 'tne
    '            IndexofRecord(temp, 8, "^"c, s)
    '            tempresult = tempresult + Chr(9) + s 'loai
    '        End If
    '    End If
    '    IndexofRecord(ResultRecord, 4, "|"c, s) 'gia tri
    '    tempresult = tempresult + Chr(9) + s
    '    IndexofRecord(ResultRecord, 5, "|"c, s) 'don vi
    '    tempresult = tempresult + Chr(9) + s
    '    IndexofRecord(ResultRecord, 7, "|"c, s) 'flag
    '    tempresult = tempresult + Chr(9) + s
    '    IndexofRecord(ResultRecord, 13, "|"c, s) 'ngay
    '    tempresult = tempresult + Chr(9) + s.Substring(0, 4) + "/" + s.Substring(4, 2) + "/" + s.Substring(6, 2) + " " + s.Substring(8, 2) + ":" + s.Substring(10, 2) + ":" + s.Substring(12, 2)

    '    Return tempresult

    'End Function 'Trả lại các thông số cơ bản của một két quả trong một Record Result
    'Public Function Testresultstring(ByVal Records() As String) As String() 'Tìm tất cả các Record Result trong message Và trả lại các kết quả xét nghiệm trong một mảng string
    '    Dim i As Integer = -1
    '    Dim s As String
    '    For Each s In Records
    '        If ((s(0) = "R"c) Or (s(0) = "r"c)) Then
    '            i = i + 1
    '        End If
    '    Next
    '    Dim result(i) As String
    '    i = 0
    '    For Each s In Records
    '        If ((s(0) = "R"c) Or (s(0) = "r"c)) Then
    '            result(i) = Test(s)
    '            i = i + 1
    '        End If
    '    Next
    '    Return result
    'End Function
    'Public Function Testresult(ByVal TestString As String) As String()
    '    Dim delimiterchar As Char() = {" "c}
    '    Return TestString.Split(delimiterchar)

    'End Function

    'Public Function So_Benh_nhan(ByVal Str() As String) As Integer()
    '    Try
    '        Dim m As Integer = 0
    '        Dim j As Integer

    '        If Str Is Nothing Then

    '        Else
    '            For j = 0 To Str.GetUpperBound(0)
    '                If Len(Str(j)) > 2 Then
    '                    If Str(j).Chars(0) = "P" Then

    '                        m = m + 1
    '                    End If
    '                End If
    '            Next

    '            Dim a(m - 1) As Integer
    '            Dim s As Integer = 0

    '            For j = 0 To Str.GetUpperBound(0)

    '                If Len(Str(j)) > 2 Then
    '                    If Str(j).Chars(0) = "P" Then

    '                        a(s) = j
    '                        s = s + 1
    '                    End If
    '                End If

    '            Next
    '            Return a
    '        End If

    '    Catch ex As Exception

    '    End Try

    'End Function
    'Public Function Xu_Ly(ByVal StrInput As String) As Ket_qua()

    '    Dim frame1 As String() = {}
    '    Dim message As String = ""
    '    Dim record As String() = {}
    '    frame1 = frame(StrInput)
    '    message = CleanString(frame1)
    '    record = Records(message)
    '    Dim testresult() As String = {}
    '    Dim ten As String = ""
    '    Dim Tendem As String = ""
    '    Dim ho As String = ""
    '    Dim tuoi As String = ""
    '    Dim PID As String = ""
    '    Dim gioitinh As String = ""
    '    Dim SID As String = ""
    '    Dim ChuThich As String = ""
    '    Dim j As Integer = So_Benh_nhan(record).GetUpperBound(0)
    '    Dim bn(j) As Ket_qua
    '    Dim k As Integer
    '    Dim vt(j + 1) As Integer
    '    For k = 0 To j
    '        vt(k) = So_Benh_nhan(record)(k)
    '    Next
    '    vt(j + 1) = record.GetUpperBound(0) + 1

    '    For k = 0 To j
    '        Dim rc As Integer = vt(k + 1) - vt(k)
    '        Dim record1(rc - 1) As String
    '        Dim rc1 As Integer
    '        For rc1 = 0 To rc - 1
    '            record1(rc1) = record(rc1 + vt(k))
    '        Next
    '        Patient(record1, PID, gioitinh, ten, ho, Tendem, tuoi)
    '        bn(k).PID = PID
    '        bn(k).Gioi_tinh = gioitinh
    '        bn(k).Ten = ten
    '        bn(k).Ten_dem = Tendem
    '        bn(k).Ho = ho
    '        bn(k).SID = SampleID(record1)
    '        Dim s As String
    '        Dim i As Integer = 0
    '        Dim str As String()
    '        testresult = Testresultstring(record1)
    '        ReDim bn(k).Ket_qua(testresult.Length() - 1)
    '        ReDim bn(k).Tenxetnghiem(testresult.Length() - 1)
    '        ReDim bn(k).Don_vi(testresult.Length() - 1)
    '        ReDim bn(k).ngay_xet_nghiem(testresult.Length() - 1)
    '        For Each s In testresult
    '            str = s.Split(Chr(9))
    '            bn(k).Ket_qua(i) = str(2)
    '            bn(k).Don_vi(i) = str(3)
    '            bn(k).Tenxetnghiem(i) = str(0) + "." + str(1)
    '            bn(k).ngay_xet_nghiem(i) = str(5)
    '            i = i + 1
    '        Next

    '        Dim st As String = ""
    '        Dim sTemp As String = ""
    '        For Each st In record1
    '            If (st(0) = "C") Then
    '                sTemp = st
    '                Exit For
    '            End If
    '        Next
    '        If (sTemp.Length() > 2) Then
    '            If (IndexofRecord(sTemp, 3, "|"c, ChuThich)) Then
    '            Else
    '                ChuThich = ""
    '            End If
    '        End If

    '    Next

    'End Function
End Class
